<template>
  <el-row>
    <el-card>
      <el-button-group>
        <el-button
          v-for="(item, index) in $t('collectionManagement.stageReport.reportType')"
          :key="index"
          :type="chType === item['value'] ? 'primary' : ''"
          @click="changeStatus(item['value'])"
        >
          {{ item['desc'] }}
        </el-button>
      </el-button-group>
      <el-divider />
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="$t('collectionManagement.stageReport.date')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.stageReport.reBorrow')">
          <el-select v-model="searchForm.firstLoan" clearable :placeholder="$t('collectionManagement.report.prompt.select')">
            <el-option
              v-for="item in $t('collectionManagement.report.borrowData')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          v-if="chType === 2"
          fixed="left"
          prop="date"
          :label="tableText['date']"
          min-width="170"
        />
        <el-table-column
          fixed="left"
          prop="stageName"
          :label="tableText['stageName']"
          min-width="150"
        />
        <el-table-column
          prop="startOverdueTime"
          :label="tableText['fromTime']"
          min-width="150"
        />
        <el-table-column
          prop="endOverdueTime"
          :label="tableText['toTime']"
          min-width="150"
        />
        <el-table-column
          prop="beforCount"
          :label="tableText['beforeStayNumber']"
          min-width="150"
        />
        <el-table-column
          prop="newlyAddCount"
          :label="tableText['addCases']"
          min-width="150"
        />
        <el-table-column
          prop="successCount"
          :label="tableText['successCases']"
          min-width="150"
        />
        <el-table-column
          prop="anewAllotCount"
          :label="tableText['distribution']"
          min-width="150"
        />
        <el-table-column
          prop="failCount"
          :label="tableText['failCases']"
          min-width="150"
        />
        <el-table-column
          prop="urgentRecallCount"
          :label="tableText['urgentCases']"
          min-width="150"
        />
        <el-table-column
          prop="surplusUrgentRecallCount"
          :label="tableText['currentCasesNumber']"
          min-width="150"
        />
        <el-table-column
          prop="urgentRecallSuccessRatio"
          :label="tableText['recallRate']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row['urgentRecallSuccessRatio'] !== null ? (scope.row['urgentRecallSuccessRatio'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="urgentRecallFailRatio"
          :label="tableText['failureRate']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row['urgentRecallFailRatio'] !== null ? (scope.row['urgentRecallFailRatio'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { getOverdueReceivableStageStatement, exportOverdueReceivableStageStatement } from '@/api/collection'
import { formatDate } from '@/utils/method'
export default {
  name: 'StageReport',
  data() {
    return {
      loading: true,
      chType: 1,
      time: [new Date(), new Date()],
      tableData: [],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('collectionManagement.stageReport')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        startDay: null,
        endDay: null,
        firstLoan: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.time = [new Date(), new Date()]
      this.searchForm = this.params()
      this.getData()
    },
    changeStatus(type) {
      this.chType = type
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['startDay'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['endDay'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['startDay'] = null
        data['endDay'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      getOverdueReceivableStageStatement(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = this.chType === 1 ? res.data['statisticsTotalVOS'] : res.data['stageStatisticsVOS']
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    exportList() {
      const data = this.searchData()
      data['exportTotalData'] = this.chType === 1
      exportOverdueReceivableStageStatement(data)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
